<?php
namespace app\admin\controller;

use app\common\model\Action as ActionModel;

/**
 * 活动控制器
 * Class Action
 * @package app\index\controller
 */
class Action extends Base
{
    /**
     * [index  活动管理]
     * @return mixed|string
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new ActionModel;
        $user = $this->user;
        $this->getPageAndSize($param);
        $map = [];
        try {
            $map['status'] = ['neq',3];
            $count = $model->getCount($map);
            $allpage = intval(ceil($count / $this->size));//总页面
            $lists = $model->getlist($map, $this->from, $this->size);
        } catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/action/index');
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        writelog('查看活动列表', 0, $user);
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,
            'lists' => $lists,
        ]);
    }
    /**
     * [postAdd  添加]
     * @return mixed|string
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new ActionModel();
        $user = $this->user;
        try{
            $id=$model->addData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/action/add');
            return admin_json(-1,'添加失败'.$e->getMessage());
        }
        writelog('添加活动'.$id, 0,$user);
        return admin_json(1,'添加成功');
    }
    /**
     * [postEdit  修改]
     * @return mixed|string
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new ActionModel();
        $user = $this->user;
        try{
            $model->editData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/action/edit');
            return admin_json(-1,'修改失败'.$e->getMessage());
        }
        writelog('修改活动'.$param['id'], 1,$user);
        return admin_json(1,'修改成功');
    }
    /**
     * [getDel 删除]
     * @return mixed|string
     * @author [C。]
     */
    public function del()
    {
        $param = input('param.');
        $model = new ActionModel();
        $user = $this->user;
        try{
            $model->editData(['status' => 3,'id' => $param['id']]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/action/del');
            return admin_json(-1,'删除失败'.$e->getMessage());
        }
        writelog('删除活动--'.$param['id'], 1,$user);
        return admin_json(1,'删除成功');
    }
    /**
     * [all 获取所有活动]
     * @return mixed|string
     * @author [C。]
     */
    public function all()
    {
        $param = input('param.');
        $model = new ActionModel();
        $map = [];
        try{
            if(!empty($param['title'])){
                $map['title'] = $param['title'];
            }
            $map['status'] = 1;
            $data = $model->getAllData($map,'id','id,title');
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/action/all');
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',$data);
    }
}